//
//  HJCodeGuideController.m
//  Sneakers
//
//  Created by 胡光健 on 2017/9/28.
//  Copyright © 2017年 mac. All rights reserved.
//

#import "HJCodeGuideController.h"
#import "HJCodeGuideCollectionCell.h"
#import "HJCodeGuideListCell.h"

static NSString * reuseIdentifier = @"CollectionCell";
@interface HJCodeGuideController ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic,strong)UITableView * tableView;
@property(nonatomic,strong)NSMutableString * brandid;
@property(nonatomic,strong)NSNumber* size;
@property(nonatomic,assign) BOOL cellHide;
@property(nonatomic,strong) NSMutableArray * brandIDArr;
@end

@implementation HJCodeGuideController
-(NSMutableArray *)brandIDArr {
    if (!_brandIDArr) {
        _brandIDArr = [NSMutableArray array];
    }
    return _brandIDArr;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self configTableView];
    self.brandid =[NSMutableString string];
    _cellHide = NO;
    _model = [HJUserManager getUserModel];
}

-(void)configTableView {
    CGFloat Y;
    if (mScreenHeight<812) {
        Y = 0;
    }else {
        Y = -34;
    }
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, Y, mScreenWidth, mScreenHeight) style:UITableViewStyleGrouped];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.tableHeaderView = [self configHeader];
    [self.tableView registerCellByNibName:@"HJCodeGuideListCell"];
    [self.view addSubview:self.tableView];
}

-(UIView *)configHeader {
    UIView * view = [[NSBundle mainBundle]loadNibNamed:@"HJCodeGuideHeader" owner:nil options:nil].firstObject;
    view.frame = CGRectMake(0, 0, mScreenWidth, 270);
    return view;
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 2;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section ==0) {
        return 1;
    }else {
        return _cellHide ? _model.brand_list.count : 4;
    }
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    HJCodeGuideCollectionCell * collectionCell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
    if (!collectionCell) {
        collectionCell = [[HJCodeGuideCollectionCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier];
    }
    if (indexPath.section==0) {
        collectionCell.sizeBlock = ^(NSString *size) {
            self.size = size.numberValue;
        };
        collectionCell.models = _model.default_size;
        return collectionCell;
    }else {
        HJCodeGuideListCell *listCell = [tableView dequeueReusableCellWithIdentifier:@"HJCodeGuideListCell"];
        listCell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        if (indexPath.row==0) {
            listCell.btn.selected = YES;
        }
        brandListModel *model = _model.brand_list[indexPath.row];
        listCell.sneakersName.text = model.brand_name;
        listCell.brand = model.brand_id.integerValue;
        
       return listCell;
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section==0) {
        return 80;
    }
    return 45;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 60;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section  {
    if (section==1) {
         return 100;
    }
    return 0.001;
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    if (section==1) {
        return [self configSectionFooterWithBtnTitle:@"确定"];
    }
    return nil;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    return [self configSectionHeaderWithTitleArr:@[@"您常穿的尺码是多少", @"您感兴趣的品牌(选填)"] section:section];
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section==1) {
        HJCodeGuideListCell * cell = (HJCodeGuideListCell *)[tableView cellForRowAtIndexPath:indexPath];
        brandListModel *model = _model.brand_list[indexPath.row];
        cell.btn.selected = !cell.btn.selected;
        if (cell.btn.selected) {
            [self.brandIDArr addObject:model.brand_id];
        }else {
            [self.brandIDArr removeObject:model.brand_id];
        }
    }
}

-(UIView *)configSectionHeaderWithTitleArr:(NSArray *)titleArr section:(NSInteger)section {
    UIView *sectionView = [[UIView alloc]init];
    sectionView.frame = CGRectMake(0, 0, mScreenWidth, 60);
    sectionView.backgroundColor = [UIColor whiteColor];
    
    UILabel * lable = [[UILabel alloc]init];
    lable.frame = CGRectMake(25, sectionView.centerY, mScreenWidth-25, 22);
    lable.text = titleArr[section];
    lable.font = HJFontSize(17);
    lable.textAlignment = NSTextAlignmentLeft;
    lable.textColor = HJColorRGB(0);
    [sectionView addSubview:lable];
    
    return sectionView;
}
-(UIView *)configSectionFooterWithBtnTitle:(NSString*)title {
    UIView * footerView = [[UIView alloc]init];
    footerView.frame = CGRectMake(0, 0, mScreenWidth, 100);
    footerView.backgroundColor = [UIColor whiteColor];
    
    UIButton * rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    rightBtn.frame = CGRectMake(mScreenWidth - 65 -25, 0, 79, 20);
    rightBtn.imageEdgeInsets = UIEdgeInsetsMake(0, 65, 0, 0);
    rightBtn.titleEdgeInsets = UIEdgeInsetsMake(0, -25, 0, 0);
    [rightBtn setTitleColor:HJColorRGB(0) forState:UIControlStateNormal];
    [rightBtn setTitle:_cellHide ? @"收起": @"更多品牌" forState:UIControlStateNormal];
    [rightBtn setImage:_cellHide ? HJImage(@"Fill 1 Copy 3") : HJImage(@"Fill 1 Copy 2") forState:UIControlStateNormal];
    rightBtn.titleLabel.font = HJFontSize(mScreenWidth/414 *13);
    [rightBtn addTarget:self action:@selector(moreBtn:) forControlEvents:UIControlEventTouchUpInside];
    [footerView addSubview:rightBtn];
    
    UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = CGRectMake(30, 30, mScreenWidth -60, 50);
    [btn setTitle:title forState:UIControlStateNormal];
    [btn setTitleColor:HJColorRGB(255) forState:UIControlStateNormal];
    [btn setBackgroundColor:HJColor(93, 188, 255)];
    [btn addTarget:self action:@selector(okBtn:) forControlEvents:UIControlEventTouchUpInside];
    [footerView addSubview:btn];
    
    return footerView;
}

-(void)moreBtn:(UIButton *)btn {
    _cellHide = !_cellHide;
    [self.tableView reloadData];
}

-(void)okBtn:(UIButton *)btn {
    [self HTTPAfterRegister];
}

-(void)HTTPAfterRegister {
    if (self.size.floatValue==0.0) {
        [self showHint:@"请选择尺码"];
        return;
    }
    NSMutableDictionary * params =[NSMutableDictionary dictionary];
    for (NSString * str in self.brandIDArr) {
        [self.brandid appendFormat:@"%@,",str];
    }
    NSString * str;
    if (self.brandid.length < 1) {
        str = @"1";
    }else {
       str = [self.brandid substringToIndex:self.brandid.length-1];
    }
    params[@"interest_brand"] = str;
    params[@"size"] = self.size;
    [TZHttpTool post:AfterRegister params:params success:^(NSDictionary *result) {
        [mUserDefaults setObject:[NSString stringWithFormat:@"%@码",self.size] forKey:@"often_size"];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.35 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            HJTabBarViewController * tabBarVC = [[HJTabBarViewController alloc]init];
            [self presentViewController:tabBarVC animated:YES completion:nil];
        });
    } failure:^(NSString *msg) {
        [self showErrorHUDWithError:msg];
    }];
}

@end
